#ifndef LOG_MANAGER_P_H
#define LOG_MANAGER_P_H

#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <QtCore/QLinkedList>
#include <QtCore/QFile>
#include "log_global.h"

namespace smslog {

class LogManager;

struct LogMessage {
  int app;
  int cls;
  int obj;
  qint64 timestamp;
  LogLevel level;
  QByteArray msg;
};

class LogManagerPrivate : public QThread {
  Q_OBJECT
public:

  LogManagerPrivate(const QString &file_path,
                    const QString &file_prefix,
                    int buffer_size,
                    int queue_size,
                    int write_time);

  ~LogManagerPrivate();

  void addMessage(LogLevel level,
                  int app,
                  int cls,
                  int obj,
                  const QByteArray &msg);

private slots:

  void writeAllMessages();

private:

  void write(int v);

  void write(quint64 v);

  void write(char v);

  void write(const QString &v);

  void write(const QByteArray &v);

  void writeFile();

  void updateFile();

  void createFile();

  void destroyFile();

  bool queueIsEmpty();

  friend class LogManager;

  QMutex locker;

  bool success_work;

  QFile *file;

  qint64 next_file_timestamp;

  QString file_path;

  QString file_prefix;

  QLinkedList<LogMessage *> messages;

  char *buffer;

  int buffer_size;

  int pos;

  int write_time;

  int queue_size;

};

} // namespace smslog

#endif // LOG_MANAGER_P_H
